#include "xaudio.h"

#include <iostream>
#include <QMutex>
#include <string>

using namespace std;

struct XTime
{
    int hour;
    int minute;
    int second;
    int msec;

    XTime(int tms)
    {
         hour = (tms/1000)/3600;
         minute = ((tms/1000)%3600)/60;
         second = (tms/1000)%(3600*60);
         msec = tms%1000;
    }

    //转字符串
    std::string ToString()
    {
        char buf[16] ={0};
        sprintf(buf,"%d:%d:%d.%d",hour,minute,second,msec);
        return buf;
    }



};

class CXAudio :public XAudio
{
   public:
    QMutex mutex;
    bool ExportA(std::string src,std::string out,int beginMs, int outMs)
    {
        //ffmpeg -i test.mp4  -ss 0:0:0.112 -t 0:0:0.100 -vn -y test.mp3
        string cmd = "ffmpeg ";
        cmd += " -i ";
        cmd +=src;
        cmd +=" ";

        if(beginMs> 0)
        {
            cmd += " -ss ";
            XTime xt(beginMs);
            cmd +=xt.ToString();
        }

        if(outMs >0)
        {
            cmd += " -t ";
            XTime xt(outMs);
            cmd +=xt.ToString();

        }

        cmd += " -vn -y ";
        cmd += out;

        cout << out << endl;

        mutex.lock();

        system(cmd.c_str());
        mutex.unlock();
        return true;
    }

};

XAudio *XAudio::Get()
{

    static CXAudio cxa;
    return &cxa;

}

bool XAudio::Merge(std::string v, std::string a, std::string out)
{
    QMutex mutex;
    //ffmpeg  -i test.avi  -i test.mp3  -c copy out.avi
    string cmd = "ffmpeg ";
    cmd += " -i ";
    cmd +=v;
    cmd += " -i ";
    cmd += a;
    cmd +=" -c copy  ";
    cmd +=out;

    cout << out << endl;

    mutex.lock();
    system(cmd.c_str());
    mutex.unlock();
    return true;

}

XAudio::XAudio()
{

}

XAudio::~XAudio()
{

}
